package controllers

import (
	"go/dao"
	"go/models"
	"go/pkg/app"
	"go/pkg/e"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

// type Punishement struct {
// 	Sno    string `gorm:"column:sno;type:varchar(255)"`
// 	P_name string `gorm:"column:p_name;type:varchar(255)"`
// 	Date   string `gorm:"column:date;type:varchar(255)"`
// }

func AddHonor(c *gin.Context) {
	m := make(map[string]string, 1024)
	c.ShouldBind(&m)
	sno := m["sno"]
	hname := m["hname"]
	date := m["date"]

	h := models.Honor{
		Sno:    sno,
		H_name: hname,
		Date:   date,
		IsDel:  false,
	}

	err := dao.AddHonor(h)

	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, h, "添加成功")
}

func DelHonor(c *gin.Context) {
	json := make(map[string]string, 1024)
	c.ShouldBind(&json)

	sno := json["sno"]
	hname := json["hname"]
	date := json["date"]

	h := models.Honor{
		Sno:    sno,
		H_name: hname,
		Date:   date,
		IsDel:  true,
	}

	db := dao.GetDB()
	err := db.Table("honor").Where("sno = ?", sno).Update("is_del", true).Error
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, h, "删除成功")
}

func UpdateHonor(c *gin.Context) {
	m := make(map[string]string, 1024)
	c.ShouldBind(&m)

	sno := m["sno"]
	hname := m["hname"]
	date := m["date"]

	h := models.Honor{
		Sno:    sno,
		H_name: hname,
		Date:   date,
		IsDel:  false,
	}

	err := dao.UpdateHonor(h)

	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, h, "更新成功")
}

func GetHonor(c *gin.Context) {
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	searchName := ""
	if arg := c.Query("searchName"); arg != "" {
		searchName = arg
	}
	HonorParam := map[string]interface{}{
		"page":       page,
		"limit":      limit,
		"searchName": searchName,
	}
	info, total, err := dao.GetHonor(HonorParam)
	//fmt.Println(info);
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "OK")
}
